﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public static class Code
    {
        private static string code;
        public static Bitmap GetCode()
        {
            Bitmap bitmap = new Bitmap(140, 40);
            Graphics graphics = Graphics.FromImage(bitmap);
            Color[] colors = { Color.Black, Color.Red, Color.Green, Color.Blue, Color.Coral, Color.HotPink };
            Random random = new Random();
            code = "";
            //创建5位验证码
            for (int i = 0; i < 4; i++)
            {
                string v = random.Next(0, 10).ToString();
                code += v;
                graphics.DrawString(v, new Font("楷体", 16f), new SolidBrush(colors[random.Next(0, 6)]), new PointF(i * 15 + 7, random.Next(0, 8)));
            }

            //创建干扰线
            for (int i = 0; i < 10; i++)
            {
                graphics.DrawLine(new Pen(colors[random.Next(0, 6)], 1), new Point(random.Next(0, bitmap.Width), random.Next(0, bitmap.Height)), new Point(random.Next(0, bitmap.Width), random.Next(0, bitmap.Height)));
            }

            //创建像素点
            for (int i = 0; i < 500; i++)
            {
                bitmap.SetPixel(random.Next(0, bitmap.Width), random.Next(0, bitmap.Height), colors[random.Next(0, 6)]);
            }

            return bitmap;
        }

        public static bool CheckCode(string input) {
            return code == input;
        }
    }
}
